package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Project;
import com.init.engineering.pojo.Subitem;
import com.init.engineering.pojo.Taskplan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface SubitemDao {

    @Results(id = "SubitemMap",value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "project",javaType = Project.class,one =
            @One(select = "com.init.engineering.dao.ProjectDao.findProjectByPid",fetchType = FetchType.EAGER))
    })
    @Select("SELECT * from subitem where status = 1")
    List<Subitem> findSubitems();


    @ResultMap("SubitemMap")
    @Select({"<script> SELECT * FROM subitem <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and sname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Subitem> findSubitemByPage(QueryPageBean queryPageBean);


    @Select({"<script> SELECT count(*) FROM subitem <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and sname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);



    @Update("UPDATE subitem set status = 0 where suid = #{suid}")
    int updateSubitemStatus(@Param("suid")String suid);


    @Update("<script>update subitem <set><if test='sname != null'>sname = #{sname},</if>" +
            "<if test='pid != null'>pid = #{pid},</if>" +
            "<if test='starttime != null'>starttime = #{starttime},</if>" +
            "<if test='endtime != null'>endtime = #{endtime},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where suid = #{suid}</script>")
    int updateSubitem(Subitem subitem);


    @Insert("insert into subitem(suid,sname,pid,starttime,endtime,updatetime,status)" +
            " values(#{suid},#{sname},#{pid},#{starttime},#{endtime},#{updatetime},1)")
    int saveSubitem(Subitem subitem);

    /**
     * 找到工程对应的所有分项工程
     * @param pid
     * @return
     */
    @Select("select * from subitem where pid = #{pid} and status = 1")
    List<Subitem> findSubitemByPid(int pid);


    @Select("select * from subitem where suid = #{suid} and status = 1")
    Subitem findSubitemBySuid(String suid);
}
